package com.tl.common.video;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.androidquery.AQuery;
import com.tl.common.util.StringUtils;
import com.tl.common.video.VideoHelper.VideoResultCallback;
import com.tl.tlcommon.R;

public class SingleVideo extends RelativeLayout {
	AQuery mAquery;
	String mVideoUrl;
	Context mContext;
	View mView;

	public VideoHelper mVideoHelper;
	onVideoListener mListener;

	Boolean mShowClose = true;
	Boolean mShowBigView = true;

	PopupWindowSelectVideo mWindowSelectVideo;

	int mDefautBackground = R.drawable.ic_add_smiley;
	String mSavePath = "";

	int mHeight = 120;
	int mWidth = 120;

	public interface onVideoListener {
		public void onVideoSelected(String url);
	}

	public SingleVideo(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		initView(context);
	}

	public SingleVideo(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initView(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 设置初始化参数
	 * 
	 * @param 监听器
	 *            onVideoListener , 是否显示删除按钮 boolean showBigView 是否点击出现图片查看
	 */
	public void setVideo(onVideoListener listener, boolean showClose,
			boolean showBigView) {
		mListener = listener;
		mShowClose = showClose;
		mShowBigView = showBigView;
	}

	/**
	 * 指定图片存储路径
	 * 
	 * @param savePath
	 */
	public void setSavePath(String savePath) {
		mSavePath = savePath;
		mVideoHelper.setSavePath(savePath);
	}

	public void setDefaultImg(int defaultRes) {
		mDefautBackground = defaultRes;
		mAquery.id(R.id.single_picture_pic).image(defaultRes);
	}

	private void initView(Context context) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mView = inflater.inflate(R.layout.layout_single_picture, this);

		mAquery = new AQuery(this);

		mAquery.id(R.id.single_picture_colse).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				deleteVideo();
			}
		});
		mAquery.id(R.id.single_picture_pic).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				beginVideo(v);
			}
		});

		// 实例化SelectPicPopupWindow
		mWindowSelectVideo = new PopupWindowSelectVideo((Activity) mContext,
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						mWindowSelectVideo.dismiss();
						mVideoHelper.fetchVideo(photoCallBack,
								VideoHelper.TAKE_VIDEO);
					}
				}, new OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						mWindowSelectVideo.dismiss();
						mVideoHelper.fetchVideo(photoCallBack,
								VideoHelper.SELECT_VIDEO);
					}
				});

		mVideoHelper = new VideoHelper((Activity) mContext);

		// mWidth = mView.getMeasuredWidth();
		// mHeight = mView.getMeasuredHeight();

	}

	public void setOnActivityResult(int requestCode, int resultCode, Intent data) {
		mVideoHelper.onActivityResult(requestCode, resultCode, data);
	}

	public void setImgUrl(String url) {
		showVideo(url);
	}

	public String getImgUrl() {
		return mVideoUrl;
	}

	private VideoResultCallback photoCallBack = new VideoResultCallback() {

		@Override
		public void onResult(String result) {
			// TODO Auto-generated method stub
			showVideo(result);
		}
	};

	/**
	 * 上传成功后图片
	 * 
	 * @param url
	 */
	private void showVideo(String url) {
		mVideoUrl = url;
		// mAquery.id(R.id.single_picture_pic).clicked(this);
		if (mShowClose)
			mAquery.id(R.id.single_picture_colse).visible();
		// UIHelper.ToastMessage(mContext, "上传图片成功,图片地址:" + url);
		if (mListener != null)
			mListener.onVideoSelected(url);

		// 视频的缩略图
		Bitmap bm = ThumbnailUtils.createVideoThumbnail(url,
				Images.Thumbnails.MICRO_KIND);
		if (bm != null) {
			mAquery.id(R.id.single_picture_pic).image(bm);
		}
	}

	public void deleteVideo() {
		try {
			if (!StringUtils.isEmpty(mVideoUrl)) {
				File file = new File(mVideoUrl);
				if (file.exists()) {
					file.delete();
				}
			}

			mVideoUrl = null;
			mAquery.id(R.id.single_picture_pic).image(mDefautBackground);
			mAquery.id(R.id.single_picture_colse).gone();

			if (mListener != null)
				mListener.onVideoSelected("");
		} catch (Exception e) {

		}
	}

	private void beginVideo(View v) {
		if (StringUtils.isEmpty(mVideoUrl)) {
			mWindowSelectVideo.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
		} else if (mShowBigView) {
			// 打开浏览窗口
			playVideo(mVideoUrl);
		}

	}

	private void playVideo(String videoPath) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		String strend = "";
		if (videoPath.toLowerCase().endsWith(".mp4")) {
			strend = "mp4";
		} else if (videoPath.toLowerCase().endsWith(".3gp")) {
			strend = "3gp";
		} else if (videoPath.toLowerCase().endsWith(".mov")) {
			strend = "mov";
		} else if (videoPath.toLowerCase().endsWith(".wmv")) {
			strend = "wmv";
		}

		intent.setDataAndType(Uri.parse(videoPath), "video/" + strend);
		mContext.startActivity(intent);
	}

	public void setMaxFileSiez(double size) {
		mVideoHelper.setMaxFileSiez(size);
	}

	public String getVideoFilePath() {
		return mVideoHelper.getVideoFilePath();
	}

	public String getVideoFileName() {
		return mVideoHelper.getVideoFileName();
	}
}
